package org.adream.account.rest.api;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.model.ResultModel;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.ValidateService;
import org.adream.account.util.Constant;
import org.adream.account.util.Constant.CheckCodeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 短信 邮箱验证controller
 * 
 * @author chenhaoyu
 *
 */
@Controller
@RequestMapping("/api/validate/")
public class ValidateApiController {

	@Autowired
	ValidateService validateService;

	/**
	 * 手机获取验证码
	 * 
	 * @param phone
	 * @param request
	 */
	@RequestMapping(value = "phone", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> validateGetPhoneCode(@RequestParam String phone,@RequestParam String checkCode, HttpServletRequest request) {
		//Constant.ONE 没有任何意义 只要不等于9 Constant.NINE 
		//9用来存合并账户时用的验证码session不一样
		return validateService.sendIdentifyCode(phone, request,CheckCodeType.OTHER,checkCode);
	}

	/**
	 * 邮箱获取验证码
	 * 
	 * @param email
	 * @param request
	 */
	@RequestMapping(value = "email", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> validateGetEmailCode(@RequestParam String email, HttpServletRequest request) {
		return validateService.sendIdentifyCode(email, request,CheckCodeType.EMAIL,"");
	}

	/**
	 * 验证手机验证码
	 * @param code	手机验证码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "phone", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> validatePhoneVerify(@RequestParam String code, @RequestParam String phone, HttpServletRequest request) {
		String str = validateService.verifyPhoneIdentifyCode(code, phone, request);
		if (str == null) {
			return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, "验证成功");
		} else {
			return new ResultModel<String>(ResultModel.ERR_PARAM, str, str);
		}
	}

	/**
	 * 验证邮箱验证码
	 * @param code	邮箱验证码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "email", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> validateEmailVerify(@RequestParam String code, @RequestParam String email, HttpServletRequest request) {
		if (validateService.verifyEmailIdentifyCode(code, email, request)) {
			return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, "验证成功");
		} else {
			return new ResultModel<String>(ResultModel.ERR_PARAM, ResultModel.ERR_PARAM_MSG, "验证失败");
		}
	}
}